package aad.storyboard.model;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;


public class ParticleEffect extends GraphicalObject{
	
	private ArrayList <Particle> particles;	// particles in the explosion
	//private int x, y;						// the explosion's origin
	private float gravity;					// the gravity of the explosion (+ upward, - down)
	private float wind;						// speed of wind on horizontal
	private int size;						// number of particles
	private boolean isAlive;				// whether it's still active or not
	
	public ParticleEffect(int particleNr, float x, float y, int color) {		
		
		isAlive=true;
		this.particles = ParticlePool.getInstance().checkOut(particleNr, x, y, color);	 	
	 	this.size = particleNr;
	}
	
		
	public ArrayList <Particle> getParticles() {
		return particles;
	}
	public void setParticles(ArrayList <Particle> particles) {
		this.particles = particles;
	}	
	
	public float getGravity() {
		return gravity;
	}
	public void setGravity(float gravity) {
		this.gravity = gravity;
	}
	public float getWind() {
		return wind;
	}
	public void setWind(float wind) {
		this.wind = wind;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	
	
	public boolean isAlive() {
		return isAlive;
	}
	public boolean isDead() {
		return isAlive;
	}

	public void update() {
		if (isAlive) {
			boolean isDead = true;
			for (Particle p: particles) {
				if (p.isAlive()) {
					p.update();
					isDead = false;
				}
			}
			if (isDead)
			{
				isAlive=false; 
			}
		}
	}
	
	public void update(Rect container) {
		if (isAlive) {
			boolean isDead = true;
			for (Particle p: particles) {
				if (p.isAlive()) {
					p.update(container);
					isDead = false;
				}
			}
			if (isDead)
			{
				isAlive=false;
				ParticlePool.getInstance().checkIn(particles);
			}
		}
	}

	@Override
	public void draw(Canvas canvas) {
		for(Particle p:particles) {
			if (p.isAlive()) {
				p.draw(canvas);
			}
		}
	}


	public void setAlpha(int alpha) {
	
		for (Particle p: particles)
		{
			if (alpha==0) p.setInitialColor(Color.TRANSPARENT);
			else 
				{
				p.resetColor();
				System.out.println("Resetting alpha in ParticleEffect");
				}
		}
	}
}
